package scp_client;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class ScpClient {
	
	
	public String upload(String filename,String ip) throws UnknownHostException, IOException {
		Socket socket = new Socket(ip,8008);
		BufferedReader bReader = new BufferedReader(new InputStreamReader( socket.getInputStream()));
		OutputStream outputStream = socket.getOutputStream();
		BufferedInputStream bInputStream = new BufferedInputStream((new FileInputStream(filename)));
		String[] names = filename.split("\\\\");
		outputStream.write((names[names.length-1]+"\n").getBytes());
		outputStream.write((bInputStream.available()+"\n").getBytes());
		int m=0;
		byte[] buf = new byte[20];
		int count = 0;
		while ((m=bInputStream.read(buf))!=-1) {
			count+=m;
			outputStream.write(buf,0,m);
		}
		System.out.println(count);
		System.out.println("heheh");
		String line = bReader.readLine();
		System.out.println(line);
		bInputStream.close();
		outputStream.close();
		bReader.close();
		socket.close();
		
		
		return line;
	}
	
	public static void main(String[] args) {
			try {
				new ScpClient().upload("D:\\quiz\\customersid.xml", "127.0.0.1");
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

}
